#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "robot_window.h"
#include "trace.h"
#include "main.h"
#include "netcom.h"
#include <time.h>

double ad2dis(uint16_t advalue)
{
	double dis = 0.0;

	if (advalue > 4096 || advalue <= 0) {
		dis = 0.0;
	} else {
		dis = 21.600000000001 / (((double) advalue *3.0) / 4028.0 -
				0.17000000000001);
		
		if ( dis > 80.0 ) {
			dis = 0.810000000005;
		} else if ( dis < 10.0 ) {
			dis = 0.089999999999997;
		} else {
			dis /= 100.0;	
		}
	}

	return dis;
}

static gboolean update_sensors(cb_widgets_t *cb_widgets)
//void update_sensors(cb_widgets_t *cb_widgets)
{
	int i = 0;

//	srand(time(0));

//	while(1) {
		//gdk_threads_enter();
		for (i = 0; i < CUSTOMAD_COUNT; i++) {
			gtk_progress_bar_set_fraction(
				GTK_PROGRESS_BAR(cb_widgets->sensors_ad[i]), 
				ad2dis(cb_widgets->conn->data->custom_ad[i]));
		}
		for (i = 0; i < US_COUNT; i++) {
			gtk_progress_bar_set_fraction(
			 GTK_PROGRESS_BAR(cb_widgets->sensors_us[i]), 
			  (double)cb_widgets->conn->data->us_range[i]/300);
		}

		for (i = 0; i< ENCODER_COUNT; i++) {
			gchar text[10];
			sprintf(text, "%d", 
			cb_widgets->conn->data->encoder_count[i]);

			gtk_entry_set_text(
			 GTK_ENTRY(cb_widgets->encoder_pulse[i]), text);
			memset(text, 0, 10);
		}
//		usleep(655500); // at 600ms without US works nice
				// at 650ms without EN works almost nice 
		//TRACE("Bars changed");
		//gdk_flush();
		//gdk_threads_leave();
#ifdef DEBUG
//		g_print("updated\n");
#endif
//	}
	return TRUE;
}

static gboolean delete_event(GtkWidget *widget, 
				GdkEvent *event, 
				gpointer data)
{
	gtk_main_quit();

	return FALSE;
}

static void ping_clicked(GtkWidget *widget, gpointer data)
{
	char address[20];
	int port;
	cb_widgets_t *cb_widgets = (cb_widgets_t *)data;

	strcpy(address, gtk_entry_get_text(GTK_ENTRY(cb_widgets->address)));
	port = atoi(gtk_entry_get_text(GTK_ENTRY(cb_widgets->port)));

	ping_robot(address, port, &cb_widgets->conn);
//	g_thread_create((GThreadFunc)update_sensors, (void *) cb_widgets, 
//						TRUE, NULL);
	g_timeout_add(500, (GSourceFunc)update_sensors, cb_widgets);
}

static void exec_clicked(GtkWidget *widget, gpointer data)
{
	int action_type = 0, amount = 0;
	cb_widgets_t *cb_widgets = (cb_widgets_t *)data;

	action_type = 
		gtk_combo_box_get_active(GTK_COMBO_BOX(cb_widgets->action));
	amount = atoi(gtk_entry_get_text(GTK_ENTRY(cb_widgets->amount)));

	exec_action(action_type, amount, cb_widgets->conn);
}

static void halt_clicked(GtkWidget *widget, gpointer data)
{
	send_halt();	
}

GtkWidget *robot_window_new(cb_widgets_t *cb_widgets)
{
	GtkWidget *window = NULL;
	GtkWidget *vbox = NULL, *hbox[5];
	GtkWidget *addresse = NULL, *porte = NULL, *pingb = NULL;
	GtkWidget *sensore[CUSTOMAD_COUNT];
	GtkWidget *sensor_usp[US_COUNT];
	GtkWidget *encoder_pulsee[ENCODER_COUNT];
	GtkWidget *actionc = NULL, *amounte = NULL, *execb = NULL;
	GtkWidget *haltb = NULL;
	GtkWidget *hseparator = NULL;
	GtkWidget *image = NULL;
	int i = 0;

	/* Constructors */

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	vbox = gtk_vbox_new(FALSE, 10);
	hbox[0] = gtk_hbox_new(FALSE, 0);
	hbox[1] = gtk_hbox_new(FALSE, 0);
	hbox[2] = gtk_hbox_new(FALSE, 0);
	hbox[3] = gtk_hbox_new(FALSE, 0);
	hbox[4] = gtk_hbox_new(FALSE, 0);
	addresse = gtk_entry_new();
	porte = gtk_entry_new();
	pingb = gtk_button_new_with_label("connect");
	actionc = gtk_combo_box_new_text();	
	execb = gtk_button_new_with_label("execute");
	haltb = gtk_button_new_with_label("halt");
	hseparator = gtk_hseparator_new();
	image = gtk_image_new_from_file("mug-tux.jpg");

	/* Seters */

	gtk_window_set_title(GTK_WINDOW(window), "Robot Command Panel");
	gtk_entry_set_text(GTK_ENTRY(addresse), "192.168.0.202");
	gtk_entry_set_text(GTK_ENTRY(porte), "10001");

	for (i = 0; i<CUSTOMAD_COUNT; i++) {
		//sensore[i] = gtk_entry_new();
		char text[10];
		sensore[i] = gtk_progress_bar_new();
		gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(sensore[i]),
					GTK_PROGRESS_BOTTOM_TO_TOP);
		gtk_container_add(GTK_CONTAINER(hbox[1]), sensore[i]);

		sprintf(text, "IR %d", i);
		gtk_progress_bar_set_text(GTK_PROGRESS_BAR(sensore[i]),
					text);	
		gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(sensore[i]),
					(double) 1/( 1 + i) );
		gtk_widget_show(sensore[i]);
	}

	for ( i = 0; i<US_COUNT; i++ ) {
		char text[10];
		sensor_usp[i] = gtk_progress_bar_new();
		gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(
							sensor_usp[i]),
					GTK_PROGRESS_BOTTOM_TO_TOP);
		gtk_container_add(GTK_CONTAINER(hbox[3]), sensor_usp[i]);

		sprintf(text, "US %d", i);
		gtk_progress_bar_set_text(GTK_PROGRESS_BAR(sensor_usp[i]),
					text);	
		gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(sensor_usp[i]),
					(double) 2/( 1 + i*i) );
		gtk_widget_show(sensor_usp[i]);
	}

	for ( i = 0; i < ENCODER_COUNT; i++ ) {
		encoder_pulsee[i] = gtk_entry_new();
		gtk_entry_set_text(GTK_ENTRY(encoder_pulsee[i]), "0");
		gtk_container_add(GTK_CONTAINER(hbox[4]), encoder_pulsee[i]);
		gtk_widget_show(encoder_pulsee[i]);
	}

	gtk_container_add(GTK_CONTAINER(hbox[2]), actionc);
	gtk_combo_box_append_text(GTK_COMBO_BOX(actionc), "forwards");
	gtk_combo_box_append_text(GTK_COMBO_BOX(actionc), "backwards");
	gtk_combo_box_append_text(GTK_COMBO_BOX(actionc), "rotate left");
	gtk_combo_box_append_text(GTK_COMBO_BOX(actionc), "rotate right");
	gtk_combo_box_set_active(GTK_COMBO_BOX(actionc), 0);

	amounte = gtk_entry_new();
	gtk_entry_set_text(GTK_ENTRY(amounte), "1000");

	/* containers */
	
	gtk_container_set_border_width(GTK_CONTAINER(window), 10);
	gtk_container_add(GTK_CONTAINER(window), vbox);
	gtk_container_add(GTK_CONTAINER(vbox), hbox[0]);
	gtk_container_add(GTK_CONTAINER(hbox[0]), addresse);
	gtk_container_add(GTK_CONTAINER(hbox[0]), porte);
	gtk_container_add(GTK_CONTAINER(hbox[0]), pingb);
	gtk_container_add(GTK_CONTAINER(vbox), hbox[1]);
	gtk_container_add(GTK_CONTAINER(vbox), hbox[2]);
	gtk_container_add(GTK_CONTAINER(vbox), hbox[3]);
	gtk_container_add(GTK_CONTAINER(vbox), hbox[4]);
	gtk_container_add(GTK_CONTAINER(hbox[2]), amounte);
	gtk_container_add(GTK_CONTAINER(hbox[2]), execb);
	gtk_container_add(GTK_CONTAINER(hbox[2]), haltb);
	gtk_container_add(GTK_CONTAINER(vbox), hseparator);
	gtk_container_add(GTK_CONTAINER(vbox), image);

	/* setup callback widgets */

	cb_widgets->address = addresse;
	cb_widgets->port = porte;
	cb_widgets->action = actionc;
	cb_widgets->amount = amounte;
	for (i = 0; i < CUSTOMAD_COUNT; i++) {
		cb_widgets->sensors_ad[i] = sensore[i];
	}
	for (i = 0; i < US_COUNT; i++) {
		cb_widgets->sensors_us[i] = sensor_usp[i];
	}
	for (i = 0; i < ENCODER_COUNT; i++) {
		cb_widgets->encoder_pulse[i] = encoder_pulsee[i];
	}

	/* Signals */

	g_signal_connect(window, "delete-event", 
			G_CALLBACK(delete_event), NULL);
	g_signal_connect(pingb, "clicked", 
			G_CALLBACK(ping_clicked), (gpointer) cb_widgets);
	g_signal_connect(execb, "clicked",
			G_CALLBACK(exec_clicked), (gpointer) cb_widgets);
	g_signal_connect(haltb, "clicked",
			G_CALLBACK(halt_clicked), (gpointer) cb_widgets);

	/* revealers */

	gtk_widget_show(addresse);
	gtk_widget_show(porte);
	gtk_widget_show(pingb);
	gtk_widget_show(actionc);
	gtk_widget_show(amounte);
	gtk_widget_show(execb);
	gtk_widget_show(haltb);
	gtk_widget_show(hseparator);
	gtk_widget_show(image);
	gtk_widget_show(hbox[0]);
	gtk_widget_show(hbox[1]);
	gtk_widget_show(hbox[2]);
	gtk_widget_show(hbox[3]);
	gtk_widget_show(hbox[4]);
	gtk_widget_show(vbox);
	gtk_widget_show(window);


	return window;
}
